package com.zescs.papillon.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: 594781919@qq.com
 * @Date: 2018/4/8 15:26
 * @version: 1.0
 */
@Component
public class MyUserDetailsService implements UserDetailsService {
	
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO 这个地方可以通过username从数据库获取正确的用户信息，包括密码和权限等。
		List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
		grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER"));
		grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		System.out.println("有请求接入进来::"+username);
		System.out.println(passwordEncoder.encode("123456"));
		System.out.println("密码比对结果::"+passwordEncoder.matches("123456", "$2a$04$maL7JgJLoIwqa9BHuCosce/FzUWvIk2rJ9xnt82lXblIDepyrFUVy"));
		return new User(username, passwordEncoder.encode("123456"), grantedAuthorityList);
	}
}
